<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" id="customerForm">
    <input name="id" id="id" type="hidden"/>
    <input name="productImg" id="productImg" type="hidden"/>
    <br/>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">考场名称</label>
        <div class="layui-input-inline">
            <select name="examineeHallId" id="examineeHallId" lay-filter="examineeHallId">
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">考试名称</label>
        <div class="layui-input-inline">
            <input type="text" name="examineeSubject" id="examineeSubject" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">考试开始时间</label>
        <div class="layui-input-inline">
            <input type="text" name="examineeStartTime" id="examineeStartTime" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">考试结束时间</label>
        <div class="layui-input-inline">
            <input type="text" name="examineeEndTime" id="examineeEndTime" autocomplete="off" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">提前考试时间</label>
        <div class="layui-input-inline">
            <select name="examineeHallId" id="beforeTime" lay-filter="beforeTime">
                <option value="0">0分钟</option>
                <option value="15">15分钟</option>
                <option value="30">30分钟</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label" style="width: 120px">迟到无法入场时间</label>
        <div class="layui-input-inline">
            <select name="examineeHallId" id="afterTime" lay-filter="afterTime">
                <option value="0">0分钟</option>
                <option value="15">15分钟</option>
                <option value="30">30分钟</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%" id="div1">
        <label class="layui-form-label" style="width: 120px">考试人员上传:</label>
        <div class="layui-inline">
            <input type="file" accept=".csv" name="csvfile1" value="选择文件">
        </div>
    </div>
    <div class="layui-form-item" style="margin-left: 20%" id="div2">
        <label class="layui-form-label" style="width: 120px">缴费信息上传:</label>
        <div class="layui-inline">
            <input type="file" accept=".csv" name="csvfile2" value="选择文件">
        </div>
    </div>
    <button class="layui-btn" id="formSubmit" lay-submit="" lay-filter="formSubmit" style="display: none">立即提交
    </button>
</form>
<table class="layui-table" id="grid1" lay-filter="grid1">
</table>
<table class="layui-table" id="grid2" lay-filter="grid2" hidden>
</table>
<script th:replace="/common/inc::incJs"></script>
<style type="text/css">
    .w-e-text-container {
        height: 500px !important;
        width: 700px /*!important是重点，因为原div是行内样式设置的高度300px*/
    }
</style>
<script>
    var str = [];
    var str2 = [];
    var form = layui.form;
    var successCallback;
    var id = getQueryString("id");
    $("#id").val(id);


    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#examineeStartTime', //指定元素
            type: 'datetime'
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#examineeEndTime', //指定元素
            type: 'datetime'
        });
    });

    //监听提交
    form.on('submit(formSubmit)', function (data) {
        data.field.examineeHallName = $("#examineeHallId").find("option:selected").text();
        if (id != "") {
            $.post('/backend/security/examineeArrange/updateExamineeArrange', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                } else {
                    layer.msg('操作成功');
                    successCallback && successCallback();
                }
            });
        } else {
            if (str2 != []) {
                data.field.isPay = 1;
            }
            $.post('/backend/security/examineeArrange/addExamineeArrange', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                } else {
                    var dataStr = str;
                    var dataStr2 = str2;
                    saveImport(dataStr, dataStr2);

                    //执行数据库存储过程,将数据表格
                    function saveImport(data) {
                        $.ajax({
                            type: 'POST',
                            url: '/backend/security/examineeArrange/importExamineeArrange',
                            data: {
                                data: JSON.stringify(dataStr),
                                data2: JSON.stringify(dataStr2),
                                examineeArrangeId: res.body.id
                            },
                            success: function (res) {
                                if (res.code < 0) {
                                    layer.msg(res.message);
                                } else {

                                }
                            },
                            dataType: 'json',
                            timeout: 600000
                        });
                    }

                    openDialog({
                        url: "/backend/security/examineeArrange/list3?examineeArrangeRecordId=" + res.body.id,
                        area: ["75%", "90%"],
                        btn: ["开始考试"]
                    });
                }
            });
        }
        return false;
    });

    var formSubmit = function (callback) {
        successCallback = callback;
        $("#formSubmit").click();
    };


    //生成数据表格s
    function csv() {
        str = [];
        var errorNo = [];
        $("input[name=csvfile1]").csv2arr(function (arr) {
            $.each(arr, function (i, line) {
                if (i == 0) {
                    return true;
                }

                var status = 0;
                if (line[6] == "初试") {
                    status = 0;
                } else if (line[6] == "复试") {
                    status = 1;
                } else if (line[6] == "补考") {
                    status = 2;
                }

                str.push({
                    examineeName: line[0],
                    idCard: line[7],
                    account: line[2],
                    sex: line[1],
                    userId: line[3],
                    examineeInstitution: line[5],
                    status: status
                });

            });
            //alert(JSON.stringify(errorNo));
        });
    }

    $("input[name=csvfile1]").change(function () {
        csv();
    });

    //生成数据表格s
    function csv2() {
        str2 = [];
        var errorNo = [];
        $("input[name=csvfile2]").csv2arr(function (arr) {
            $.each(arr, function (i, line) {
                if (i == 0) {
                    return true;
                }

                str2.push({
                    examineeName: line[0],
                    idCard: line[2]
                });

            });
            //alert(JSON.stringify(errorNo));
        });
    }

    $("input[name=csvfile2]").change(function () {
        csv2();
    });


    //初始化加载
    $(function () {
        $.post('/backend/security/examineeHall/getAllHall', function (res) {
            if (res.code < 0) {
                layer.msg(res.message);
            } else {
                $.each(res.body, function (i, item) {
                    $("#examineeHallId").append("<option value='" + item.id + "'>" + item.examineeHallName + "</option>");
                });
                form.render('select');

                if (id != "") {
                    $("#div1").hide();
                    $("#div2").hide();
                    $.post('/backend/security/examineeArrange/getById', {id: id}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        } else {
                            $("form").loadData(res.body);
                            layui.form.render('select');
                        }
                    });
                }
            }
        });
    })
</script>
</body>
</html>
